package cc.magicjson.synthesis.application;



import cc.magicjson.synthesis.decorator.CompositeDiscountDecorator;
import cc.magicjson.synthesis.factory.OrderDiscountDecoratorFactory;
import cc.magicjson.synthesis.model.Coupon;
import cc.magicjson.synthesis.model.Order;
import cc.magicjson.synthesis.model.User;
import cc.magicjson.synthesis.service.DiscountService;
import cc.magicjson.synthesis.service.PaymentService;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 应用程序主类，演示聚合优惠支付功能
 */
public class Application {

    public static void main(String[] args) {
        // 创建优惠券
        List<Coupon> coupons = new ArrayList<>();
        coupons.add(new Coupon("c001", "满50减10券",
                new BigDecimal("50"), new BigDecimal("10"),
                LocalDateTime.now().minusDays(1),
                LocalDateTime.now().plusDays(30)));
        coupons.add(new Coupon("c002", "满100减30券",
                new BigDecimal("100"), new BigDecimal("30"),
                LocalDateTime.now().minusDays(1),
                LocalDateTime.now().plusDays(10)));

        // 创建用户
        User user = new User("u001", "张三", 2000, coupons);

        // 创建订单
        Order order = new Order("o001", new BigDecimal("120"), user);

        // 第一种方式：使用责任链模式计算折扣
        System.out.println("=== 使用责任链模式计算折扣 ===");
        DiscountService discountService = new DiscountService(100); // 100积分抵1元
        Order discountedOrder = discountService.calculateDiscount(order);
        System.out.println("原始金额：" + discountedOrder.getOriginalAmount());
        System.out.println("优惠金额：" + discountedOrder.getDiscountAmount());
        System.out.println("最终金额：" + discountedOrder.getFinalAmount());

        // 重置订单金额，准备使用装饰器模式
        order.setFinalAmount(order.getOriginalAmount());
        order.setDiscountAmount(BigDecimal.ZERO);

        // 第二种方式：使用装饰器模式计算折扣
        System.out.println("\n=== 使用装饰器模式计算折扣 ===");
        CompositeDiscountDecorator decorator =
                OrderDiscountDecoratorFactory.createCompositeDecorator(order, 100);
        decorator.applyDiscount();

        System.out.println("原始金额：" + order.getOriginalAmount());
        System.out.println("优惠金额：" + order.getDiscountAmount());
        System.out.println("最终金额：" + order.getFinalAmount());
        System.out.println(decorator.getDiscountDescription());

        // 支付订单
        System.out.println("\n=== 支付订单 ===");
        PaymentService paymentService = new PaymentService(discountService);
        boolean paymentResult = paymentService.processPayment(order);
        System.out.println("支付结果：" + (paymentResult ? "成功" : "失败"));
    }
}
